package com.class04.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @ClassDescription:
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2023/11/7 15:51
 */
@Component
@Aspect
public class TxAroundAdvice {
    /*环绕通知，需要你在通知中，自定义法方法执行
    @param  joinPoint  目标方法(获取目标方法信息，多了执行的一个方法)
    return 目标方法的返回值
    * */
    @Around("com.class04.pointcut.MyPointCut.pc()")
    public Object transaction(ProceedingJoinPoint joinPoint) {
//        保证目标方法被执行即可
        Object[] args = joinPoint.getArgs();
        Object result = null;
        try {
            //增强代码  -> before
            System.out.println("开启事务");
            result = joinPoint.proceed(args);
            //增强代码  -> afterReturning
            System.out.println("结束事务,获取结果：" + result);
        } catch (Throwable e) {
            //增强代码  ->  afterThrowing
            System.out.println("事务回滚");
            throw new RuntimeException(e);
        } finally {
            //增强代码  ->  after
        }
        return result;
    }
}
